package com.example.zg.zg_312_ordering_new.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.activity.FoodInfoActivity;
import com.example.zg.zg_312_ordering_new.activity.FoodListActivity;
import com.example.zg.zg_312_ordering_new.beans.FoodInfo;
import com.example.zg.zg_312_ordering_new.beans.Shop;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List <FoodInfo> data;
    private LayoutInflater layoutInflater;
    private String phonenum;

    public FoodListAdapter(Context context,List data,int layoutResource,String phonenum){
        this.context=context;
        this.data=data;
        this.phonenum=phonenum;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_food, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final FoodInfo entity = data.get(position);
        if (null == entity) return;
        ViewHolder viewHolder = (ViewHolder) holder;


        viewHolder.food_foodname.setText(entity.getFoodname());
        viewHolder.food_intro.setText(entity.getIntro());
        viewHolder.food_price.setText(entity.getPrice() + "元");
        Picasso.get().load(Constants.BASE_URL + entity.getPic()).into(viewHolder.food_pic);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, FoodInfoActivity.class);
                intent.putExtra("id", entity.getFood_id());

                intent.putExtra("phonenum",phonenum);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        ImageView food_pic;
        TextView food_foodname, food_price, food_intro;
        LinearLayout food_container;

        public ViewHolder(View itemView) {
            super(itemView);
            food_pic = itemView.findViewById(R.id.food_pic);
            food_price = itemView.findViewById(R.id.food_price);
            food_intro = itemView.findViewById(R.id.food_intro);
            food_foodname = itemView.findViewById(R.id.food_foodname);
            food_container=itemView.findViewById(R.id.recyclerView_food_comment);

        }
    }
}

